<?php

namespace App\Http\Controllers\Index;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller{
    public function user(Request $request){
        $data=$request['data'];
        $rules = ['data.code' => 'required|captcha'];
        $validator = validator()->make(request()->all(), $rules);
        if ($validator->fails()) {
            return 0;
        }
        $rows=DB::table('index_user')
            ->where([
                "name"=>$data['name'],
                "userpwd"=>md5($data['userpwd'])
            ])
            ->limit(1)
            ->first();
        if($rows){
            $time['logintime']=date("Y-m-d H:i:s",time());
            $rows=DB::table('index_user')->where('name',$data['name'])->update($time);
            session(['name'=>$data['name']]);
            return 1;
        }
        return -1;
    }
    public function register_add(Request $request){
        $data=$request['data'];
        $rules = ['data.code' => 'required|captcha'];
        $validator = validator()->make(request()->all(), $rules);
        if ($validator->fails()) {
            return 0;
        }
        $rows2=DB::table('index_user')->where('name',$data['name'])->first();
        if($rows2){
            return -1;
        }
        unset($data['code']);
        unset($data['reuserpwd']);
        $data['userpwd']=md5($data['userpwd']);

        $data['createtime']=date("Y-m-d H:i:s",time());
        $rows=DB::table('index_user')->insert($data);
        if($rows){
            return 1;
        }
    }
}